package exception;

import java.util.Scanner;

public class Login {
    public static String USER="浩民";
    public static String PASSWORD="123456";

    public static void main(String[] args) {
        try {
            login();
            System.out.println("登陆成功！！");
        }catch (UserNameException e){
            e.printStackTrace();
        }catch (PasswordException e){
            e.printStackTrace();
        }
    }

    //throws 用在方法声明上，明确表示该方法可能会产生异常，但是不处理该异常，抛给调用者处理
    private static void login() throws UserNameException{
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String user=scanner.next();
        System.out.println("请输入用户名密码：");
        String password=scanner.next();
        if(!USER.equals(user)){
            //throw 用在方法内部，人为产生异常对象并抛出
            throw new UserNameException("用户名错误！");
        }
        if(!PASSWORD.equals(password)){
            throw new PasswordException("用户名密码错误！");
        }
    }
}

//用户名异常
class UserNameException extends Exception{
    public UserNameException(String msg){
        super(msg);
    }
}

//用户名密码异常
class PasswordException extends RuntimeException{
    public PasswordException(String msg){
        super(msg);
    }
}
